'use strict';

const Controller = require('egg').Controller;

/**
 * @Controller adminRole
 */
class AdminRoleController extends Controller {

    /**
     * @Summary 获取所有的数据
     * @Router POST /api/adminRole/moduleList
     * @request formData string p 客户端传递的公钥
     * @request formData string b 请求体对应的数据
     * @Response 200 baseResponse
     * @Response 500 baseResponse
     */
    async moduleList() {
        const {
            app,
            ctx
        } = this;
        let userid = ctx.get('uid'); //获取登录者id
        try {
            if (ctx.helper.verifyToken(ctx, userid)) {
                let publicKey = ctx.request.body["p"]; //客户端传递的公钥
                let jsonString = ctx.request.body["b"]; //请求体对应的数据
                //解密对应的加密字符串
                let jsonDecrypt = ctx.service.units.decryptDES(jsonString, publicKey);
                if (jsonDecrypt === null || jsonDecrypt === undefined || jsonDecrypt.length === 0) {
                    ctx.status = 200;
                    ctx.body = {
                        code: 0,
                        msg: "数据解析发生错误"
                    };
                } else {
                    let args = JSON.parse(jsonDecrypt);
                    const returnData = await ctx.service.adminRole.getModuleList(args);
                    const encryptDESObj = ctx.service.units.encryptDES(JSON.stringify(returnData)); //加密返回数据源
                    ctx.status = 200;
                    ctx.body = {
                        code: 1,
                        msg: "数据获取成功",
                        data: encryptDESObj.data,
                        key: encryptDESObj.publicKey
                    };
                }
            }
        } catch (error) {
            ctx.status = 500;
            ctx.body = {
                code: -1,
                msg: error.message
            };
        }
    };

    /**
     * @Summary 根据限制条件获取一条数据
     * @Router POST /api/adminRole/moduleByWhere
     * @request formData string p 客户端传递的公钥
     * @request formData string b 请求体对应的数据
     * @Response 200 baseResponse
     * @Response 500 baseResponse
     */
    async moduleByWhere() {
        const {
            app,
            ctx
        } = this;
        let userid = ctx.get('uid'); //获取登录者id
        try {
            if (ctx.helper.verifyToken(ctx, userid)) {
                let publicKey = ctx.request.body["p"]; //客户端传递的公钥
                let jsonString = ctx.request.body["b"]; //请求体对应的数据

                //解密对应的加密字符串
                let jsonDecrypt = ctx.service.units.decryptDES(jsonString, publicKey);
                if (jsonDecrypt === null || jsonDecrypt === undefined || jsonDecrypt.length === 0) {
                    ctx.status = 200;
                    ctx.body = {
                        code: 0,
                        msg: "数据解析发生错误"
                    };
                } else {
                    let args = JSON.parse(jsonDecrypt);
                    const returnData = await ctx.service.adminRole.getModuleByWhere(args);
                    const encryptDESObj = ctx.service.units.encryptDES(JSON.stringify(returnData)); //加密返回数据源
                    ctx.status = 200;
                    ctx.body = {
                        code: 1,
                        msg: "数据获取成功",
                        data: encryptDESObj.data,
                        key: encryptDESObj.publicKey
                    };
                }
            }
        } catch (error) {
            ctx.status = 500;
            ctx.body = {
                code: -1,
                msg: error.message
            };
        }
    };

    /**
     * @Summary 根据主键ID获取一条数据
     * @Router GET /api/adminRole/getModuleByPk
     * @request query string id 主键ID
     * @Response 200 baseResponse
     * @Response 500 baseResponse
     */
    async getModuleByPk() {
        const {
            app,
            ctx
        } = this;
        let userid = ctx.get('uid'); //获取登录者id
        try {
            if (ctx.helper.verifyToken(ctx, userid)) {
                let id = ctx.query["id"];
                const returnData = await ctx.service.adminRole.getModuleByPk(id);
                const encryptDESObj = ctx.service.units.encryptDES(JSON.stringify(returnData)); //加密返回数据源
                ctx.status = 200;
                ctx.body = {
                    code: 1,
                    msg: "数据获取成功",
                    data: encryptDESObj.data,
                    key: encryptDESObj.publicKey
                };
            }
        } catch (error) {
            ctx.status = 500;
            ctx.body = {
                code: -1,
                msg: error.message
            };
        }
    };

    /**
     * @Summary 分页查询数据
     * @Router POST /api/adminRole/moduleListByPage
     * @request formData number limit 页大小
     * @request formData number offset 页索引
     * @request formData string p 客户端传递的公钥
     * @request formData string b 请求体对应的数据
     * @Response 200 baseResponse
     * @Response 500 baseResponse
     */
    async moduleListByPage() {
        const {
            app,
            ctx
        } = this;
        let userid = ctx.get('uid'); //获取登录者id
        try {
            if (ctx.helper.verifyToken(ctx, userid)) {
                let limit = ctx.helper.parseInt(ctx.request.body["limit"]); //页大小
                let offset = ctx.helper.parseInt(ctx.request.body["offset"]); //页索引
                let publicKey = ctx.request.body["p"]; //客户端传递的公钥
                let jsonString = ctx.request.body["b"]; //请求体对应的数据

                //解密对应的加密字符串
                let jsonDecrypt = ctx.service.units.decryptDES(jsonString, publicKey);
                if (jsonDecrypt === null || jsonDecrypt === undefined || jsonDecrypt.length === 0) {
                    ctx.status = 200;
                    ctx.body = {
                        code: 0,
                        msg: "数据解析发生错误"
                    };
                } else {
                    let args = JSON.parse(jsonDecrypt);
                    const returnData = await ctx.service.adminRole.getModuleListByPage(offset, limit, args);
                    const encryptDESObj = ctx.service.units.encryptDES(JSON.stringify(returnData)); //加密返回数据源
                    ctx.status = 200;
                    ctx.body = {
                        code: 1,
                        msg: "数据获取成功",
                        data:encryptDESObj.data,
                        key: encryptDESObj.publicKey
                    };
                }
            }
        } catch (error) {
            ctx.status = 500;
            ctx.body = {
                code: -1,
                msg: error.message
            };
        }
    };

    /**
     * @Summary 新增数据
     * @Router POST /api/adminRole/createInfo
     * @request formData string t 需要创建的时间参数 字符串数组
     * @request formData string p 客户端传递的公钥
     * @request formData string b 需要新增的字段值对象，不包含时间类型
     * @Response 200 baseResponse
     * @Response 500 baseResponse
     */
    async createInfo() {
        const {
            app,
            ctx
        } = this;
        let userid = ctx.get('uid'); //获取登录者id
        try {
            if (ctx.helper.verifyToken(ctx, userid)) {
                let publicKey = ctx.request.body["p"]; //客户端传递的公钥
                let timeListString = ctx.request.body["t"]; //请求体对应的数据 时间
                let dataListString = ctx.request.body["d"]; //请求体对应的数据 数据

                //解密对应的加密字符串
                let timeListDecrypt = ctx.service.units.decryptDES(timeListString, publicKey);
                let dataListDecrypt = ctx.service.units.decryptDES(dataListString, publicKey);
                if ((timeListDecrypt === null || timeListDecrypt === undefined || timeListDecrypt.length === 0) ||
                    (dataListDecrypt === null || dataListDecrypt === undefined || dataListDecrypt.length === 0)
                ) {
                    ctx.status = 200;
                    ctx.body = {
                        code: 0,
                        msg: "数据解析发生错误"
                    };
                } else {
                    let timeList = JSON.parse(timeListDecrypt); //需要创建的时间参数 字符串数组
                    let dataList = JSON.parse(dataListDecrypt); //需要新增的字段值对象，不包含时间类型
                    const returnData = await ctx.service.adminRole.postCreateInfo(timeList, dataList);
                    ctx.status = 200;
                    ctx.body = {
                        code: returnData > 0 ? 1 : 0,
                        msg: returnData > 0 ? "数据操作成功" : "数据操作失败"
                    };
                }
            }
        } catch (error) {
            ctx.status = 500;
            ctx.body = {
                code: -1,
                msg: error.message
            };
        }
    };

    /**
     * @Summary 更新数据
     * @Router POST /api/adminRole/updateInfo
     * @request formData string id 需要更新的主键ID
     * @request formData string t 需要创建的时间参数 字符串数组
     * @request formData string p 客户端传递的公钥
     * @request formData string b 需要新增的字段值对象，不包含时间类型
     * @Response 200 baseResponse
     * @Response 500 baseResponse
     */
    async updateInfo() {
        const {
            app,
            ctx
        } = this;
        let userid = ctx.get('uid'); //获取登录者id
        try {
            if (ctx.helper.verifyToken(ctx, userid)) {
                let id = ctx.request.body["id"]; //需要更新的元素ID
                let publicKey = ctx.request.body["p"]; //客户端传递的公钥
                let timeListString = ctx.request.body["t"]; //请求体对应的数据 时间
                let dataListString = ctx.request.body["d"]; //请求体对应的数据 数据

                //解密对应的加密字符串
                let timeListDecrypt = ctx.service.units.decryptDES(timeListString, publicKey);
                let dataListDecrypt = ctx.service.units.decryptDES(dataListString, publicKey);
                if ((timeListDecrypt === null || timeListDecrypt === undefined || timeListDecrypt.length === 0) ||
                    (dataListDecrypt === null || dataListDecrypt === undefined || dataListDecrypt.length === 0)
                ) {
                    ctx.status = 200;
                    ctx.body = {
                        code: 0,
                        msg: "数据解析发生错误"
                    };
                } else {
                    let timeList = JSON.parse(timeListDecrypt); //需要创建的时间参数 字符串数组
                    let dataList = JSON.parse(dataListDecrypt); //需要新增的字段值对象，不包含时间类型
                    const returnData = await ctx.service.adminRole.postUpdateInfo(id, timeList, dataList);
                    ctx.status = 200;
                    ctx.body = {
                        code: returnData > 0 ? 1 : 0,
                        msg: returnData > 0 ? "数据操作成功" : "数据操作失败"
                    };
                }
            }
        } catch (error) {
            ctx.status = 500;
            ctx.body = {
                code: -1,
                msg: error.message
            };
        }
    };

    /**
     * @Summary 根据限制条件删除对应的数据
     * @Router POST /api/adminRole/removeInfoByWhere
     * @request formData string p 客户端传递的公钥
     * @request formData string b 需要查询限制字段值对象
     * @Response 200 baseResponse
     * @Response 500 baseResponse
     */
    async removeInfoByWhere() {
        const {
            app,
            ctx
        } = this;
        let userid = ctx.get('uid'); //获取登录者id
        try {
            if (ctx.helper.verifyToken(ctx, userid)) {
                let publicKey = ctx.request.body["p"]; //客户端传递的公钥
                let jsonString = ctx.request.body["b"]; //请求体对应的数据
                //解密对应的加密字符串
                let jsonDecrypt = ctx.service.units.decryptDES(jsonString, publicKey);
                if (jsonDecrypt === null || jsonDecrypt === undefined || jsonDecrypt.length === 0) {
                    ctx.status = 200;
                    ctx.body = {
                        code: 0,
                        msg: "数据解析发生错误"
                    };
                } else {
                    let args = JSON.parse(jsonDecrypt);
                    const returnData = await ctx.service.adminRole.postRemoveInfoByWhere(args);
                    ctx.status = 200;
                    ctx.body = {
                        code: returnData > 0 ? 1 : 0,
                        msg: returnData > 0 ? "数据操作成功" : "数据操作失败"
                    };
                }
            }
        } catch (error) {
            ctx.status = 500;
            ctx.body = {
                code: -1,
                msg: error.message
            };
        }
    };

    /**
     * @Summary 根据ID删除对应的数据
     * @Router POST /api/adminRole/removeInfoByPk
     * @request formData string id 主键ID
     * @Response 200 baseResponse
     * @Response 500 baseResponse
     */
    async removeInfoByPk() {
        const {
            app,
            ctx
        } = this;
        let userid = ctx.get('uid'); //获取登录者id
        try {
            if (ctx.helper.verifyToken(ctx, userid)) {
                let id = ctx.request.body["id"]; //需要更新的元素ID
                const returnData = await ctx.service.adminRole.postRemoveInfoByPk(id);
                ctx.status = 200;
                ctx.body = {
                    code: returnData > 0 ? 1 : 0,
                    msg: returnData > 0 ? "数据操作成功" : "数据操作失败"
                };
            }
        } catch (error) {
            ctx.status = 500;
            ctx.body = {
                code: -1,
                msg: error.message
            };
        }
    };
}

module.exports = AdminRoleController;
